package fragment;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.jizhangben.HomeActivity;
import com.example.jizhangben.R;
import com.example.jizhangben.databinding.FragmentZhiChuBinding;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import gengduo.WeiXinGenDuo;


public class ZhiChuFragment extends Fragment {
    private HomeActivity activity;
    private FragmentZhiChuBinding binding;
    private SQLiteDatabase sqlDate;
    List<WeiXInZhiChuBean> list;
    @SuppressLint("SimpleDateFormat")
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        activity = (HomeActivity) view.getContext();
        SharedPreferences sp = activity.getSharedPreferences("xinxi", Context.MODE_PRIVATE);
        Log.e("烟", String.valueOf(sp.getBoolean("yan", false)));
        if (sp.getBoolean("yan", false)) {
            binding.ynxi.setText("不吸烟很不错哦");
        }
        binding.meir.setText(sp.getString("tian", "无每日额度"));
        binding.shijian.setText(new SimpleDateFormat("E  HH点mm分").format(new Date()));


        sqlDate = new MySqlDate(activity).getWritableDatabase();
        chaxun();
        binding.textView2.setOnClickListener(v -> {
           if (binding.didian.getText().toString().equals("") || binding.hua.getText().toString().equals("")){
               Toast.makeText(activity.getApplicationContext(), "请填写相关信息", Toast.LENGTH_SHORT).show();
           }else {
               addshu();
               chaxun();
               binding.didian.setText("");
               binding.hua.setText("");
               Toast.makeText(activity.getApplicationContext(), "记账成功", Toast.LENGTH_SHORT).show();
           }
        });


        binding.gengduo.setOnClickListener(v ->{
            Intent intent = new Intent(getActivity(), WeiXinGenDuo.class);
            intent.putExtra("date", (Serializable) list);
            Log.e("传递数据",list.toString());
            startActivity(intent);
        });



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentZhiChuBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }
    //
    private void addshu(){
        sqlDate = new MySqlDate(activity).getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", 0);
        contentValues.put("time", new SimpleDateFormat("yyyy年MM月dd日 HH点mm分 E").format(new Date()));
        contentValues.put("didian", binding.didian.getText().toString());
        contentValues.put("money", binding.hua.getText().toString());
        contentValues.put("beizhu", binding.beizhu.getText().toString());
        sqlDate.insert("weixinzhichu", null, contentValues);
    }
    @SuppressLint("Range")
    private void chaxun(){

        Cursor cursor = sqlDate.query("weixinzhichu",null,null,null,null,null,null);
        int i = 0;
        list = new ArrayList<>();
        while (cursor.moveToNext()){
            WeiXInZhiChuBean bean = new WeiXInZhiChuBean();
            bean.setId(i++);
            bean.setTime(cursor.getString(cursor.getColumnIndex("time")));
            bean.setDidian(cursor.getString(cursor.getColumnIndex("didian")));
            bean.setMoney(cursor.getString(cursor.getColumnIndex("money")));
            bean.setBeizhu(cursor.getString(cursor.getColumnIndex("beizhu")));
            list.add(bean);
        }
        if (i == 0){
            binding.xiaofei.setText("0元");
        }else{
            Log.e("数据库数据",list.toString());
            String a = new SimpleDateFormat("dd").format(new Date());
            if(a.equals(list.get(list.size()-1).getTime().toString().substring(8,10))){
                int sum = 0;
                for (int ii = 0; ii < list.size(); ii++) {
                    sum = sum + Integer.parseInt(list.get(ii).getMoney());
                }
                binding.xiaofei.setText(sum + "元");
            }else {

                binding.xiaofei.setText("0元");
            }

        }
    }


}